package org.yxs.excel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.yxs.excel.entity.Product;
import org.yxs.excel.listener.ProductListener;
import org.yxs.excel.service.ProductService;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/export")
    public void productExtExport(HttpServletResponse response) throws Exception {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(new Date());
        String fileName = today;
        String fileNameCode = URLEncoder.encode(fileName, "UTF-8");

        response.setContentType("application/force-download");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileNameCode + ".xlsx");
        ExcelWriter writer = new ExcelWriterBuilder()
                .autoCloseStream(true)
                .file(response.getOutputStream())
                .head(Product.class)
                .build();
        Integer lastBatchMaxId = 0;
//        WriteSheet writeSheet = new WriteSheet();
//        writeSheet.setSheetName("标准商品标签导入模板" + today);
        List<Product> productExt;
        int limit = 1030000;
        int i = 0;
        for (; ; ) {
//            if (lastBatchMaxId>1030000){
//                break;
//            }
            productExt = productService.select(lastBatchMaxId, limit);
            if (productExt.isEmpty()) {
                WriteSheet writeSheet = new WriteSheet();
                writeSheet.setSheetName("标准商品标签导入模板" + today);
                writer.write(productExt, writeSheet);
                writer.finish();
                break;
            } else {
                lastBatchMaxId = productExt.stream().map(Product::getId).max(Integer::compareTo).orElse(Integer.MAX_VALUE);
                System.out.println("=======lastBatchMaxId========" + lastBatchMaxId);
                WriteSheet writeSheet = EasyExcel.writerSheet(i, today + i).build();
//                writeSheet.setSheetName("标准商品标签导入模板" + today);
                writer.write(productExt, writeSheet);
            }
            i++;

        }
    }


    @PostMapping("/import")
    public Map<String, Object> productExtImport(MultipartFile file) throws IOException {

        EasyExcel.read(file.getInputStream(), Product.class, new ProductListener(productService)).sheet().doRead();

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);

        return map;

    }


}
